package com.sgb.goods.view.adapter.publish.pop;

import android.content.Context;
import android.view.View;
import android.view.inputmethod.EditorInfo;

import androidx.databinding.ObservableField;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.KeyboardUtils;
import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.databinding.GoodsPopBrandBinding;
import com.sgb.goods.entity.BrandEntity;
import com.sgb.goods.utils.Constants;
import com.sgb.goods.utils.base.GoodsSdk;
import com.sgb.goods.view.adapter.publish.BrandAdapter;
import com.sgb.goods.widget.pop.GoodsPopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品发布-品牌弹窗
 * 注意：只适用商品module内其他业务有品牌弹窗
 *
 * @author cyj
 */
public class BrandPopWindow extends GoodsPopWindow {

    private String typeId;
    public BrandAdapter brandAdapter;
    private InputTextInterface inputTextInterface;
    private GoodsPopBrandBinding viewBinding;
    /**
     * 搜索框的数据绑定
     */
    public ObservableField<String> contentText = new ObservableField<>("");

    public BrandPopWindow(Context context) {
        super(context, DIR_DOWN_UP);
    }

    @Override
    protected int popLayout() {
        brandAdapter = new BrandAdapter();
        return R.layout.goods_pop_brand;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        holder.setBinding(BR.bpw, this);
        viewBinding = ((GoodsPopBrandBinding) holder.getmBinding());
        int type = SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 0);
        if (1 == type) {
            ((GoodsPopBrandBinding) holder.getmBinding()).grayLine.setVisibility(View.VISIBLE);
            ((GoodsPopBrandBinding) holder.getmBinding()).llAdd.setVisibility(View.VISIBLE);
        } else {
            ((GoodsPopBrandBinding) holder.getmBinding()).llAdd.setVisibility(View.GONE);
            ((GoodsPopBrandBinding) holder.getmBinding()).grayLine.setVisibility(View.GONE);
        }
        initListener((GoodsPopBrandBinding) holder.getmBinding());
    }

    private void initListener(GoodsPopBrandBinding mDataBinding) {
        mDataBinding.okView.setOnClickListener(v -> {
            // 记录本次有操作的记录点；若没操作点击，那么等效于无。
            brandAdapter.setRecordPoint();
            if (onResultClick != null && null != brandAdapter.getCurrentObj()) {
                onResultClick.result(brandAdapter.getCurrentObj().id, brandAdapter.getCurrentObj().name, null);
            }
            dissMiss();
        });
        mDataBinding.resetView.setOnClickListener(v -> {
            // 取消本次有操作的记录点；若没操作点击，那么等效于无。
            brandAdapter.cancelRecordPoint();
            dismiss();
        });
        // 添加品牌
        mDataBinding.llAdd.setOnClickListener(view -> {
            ARouter.getInstance().build(Constants.ARouter.AR_ADD_BRAND)
                    .navigation(GoodsSdk.getInstance().getTopActivity(), 666);
        });
        mDataBinding.etView.setOnEditorActionListener((textView, actionId, keyEvent) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                KeyboardUtils.hideSoftInput(textView);
                inputTextInterface.input(this.typeId);
            }
            return false;
        });
    }

    /**
     * 设置数据
     *
     * @param data   数据源
     * @param typeId 用于跳转添加品牌
     */
    public void setData(List<BrandEntity> data, String typeId) {
        this.typeId = typeId;
        if (data == null) {
            viewBinding.blackView.setVisibility(View.VISIBLE);
            viewBinding.skillList.setVisibility(View.GONE);
        } else {
            List<BrandEntity.BrandChild> childList = new ArrayList<>();
            for (BrandEntity entity : data) {
                childList.addAll(entity.childList);
            }
            if (childList.size() == 0) {
                viewBinding.blackView.setVisibility(View.VISIBLE);
                viewBinding.skillList.setVisibility(View.GONE);
            } else {
                viewBinding.blackView.setVisibility(View.GONE);
                viewBinding.skillList.setVisibility(View.VISIBLE);
            }
        }
        brandAdapter.setNewData(data);
    }

    public interface InputTextInterface {
        void input(String s);
    }

    public void inputTextInterface(InputTextInterface inputTextInterface) {
        this.inputTextInterface = inputTextInterface;
    }

}
